<?php

namespace app\admin\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use app\common\library\Id as L_ID;
use think\cache\driver\Redis;

class IdFix3 extends Command
{

    protected function configure()
    {
        $this->setName('IdFix3')
            ->setDescription('修复用户唯一ID');
    }

    protected function execute(Input $input, Output $output)
    {

        $a_list = db("user")->where("unique_id", "")->field("id")->select();
        if ($a_list) {
            //    echo count($a_list).PHP_EOL;
            foreach ($a_list as $a) {
                $id_info = db("unique_id_for_user")->where("type", 3)->where("status", 0)->find();
                //    exit(print_r($id_info));
                if ($id_info) {
                    try {
                        db()->startTrans();
                        db("user")->where("id", $a["id"])->update([
                            "unique_id" => $id_info["unique_id"]
                        ]);
                        db("unique_id_for_user")->where("id", $id_info["id"])->update([
                            "status" => 1,
                            "updatetime" => time(),
                        ]);
                        db()->commit();
                    } catch (Exception $e) {
                        echo $e->getMessage();
                        db()->rollback();
                    }
                }
            }
        }
        echo "finished" . PHP_EOL;
    }
}
